import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import sf.model.DBManager;

public class DatabaseInfo {

	private PrintWriter out;

	private DatabaseMetaData dmd;
	private ResultSet rs;
	private ResultSetMetaData md;

	private List<String> tableList;

	private String baza = DBManager.getDatabase().getDatabaseName();
	private String schema = "dbo";

	public DatabaseInfo() throws SQLException, UnsupportedEncodingException, FileNotFoundException {
		out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File("db_info.txt")), "UTF16"));
		
		System.out.println("Working ...");

		dmd = DBManager.getDatabase().getDatabaseMetaData();

		// Pronalazi sve tabele u bazi
		rs = dmd.getTables(baza, schema, "%", new String[] { "TABLE" });
		md = rs.getMetaData();
		makeTableList();

		for (String table : tableList) {
			out.println("//////////////////////////////////////////////////////////////");
			out.println("//\t" + table);
			out.println("//////////////////////////////////////////////////////////////\n");

			out.println(">>> Kolone\n");
			rs = dmd.getColumns(baza, schema, table, "%");
			md = rs.getMetaData();
			if (rs != null && md != null) {
				printHeader();
				printData();
			}

			out.println(">>> Foreign keys - imported\n");
			rs = dmd.getImportedKeys(baza, schema, table);
			md = rs.getMetaData();
			if (rs != null && md != null) {
				printHeader();
				printData();
			}

			out.println(">>> Foreign keys - exported\n");
			rs = dmd.getExportedKeys(baza, schema, table);
			md = rs.getMetaData();
			if (rs != null && md != null) {
				printHeader();
				printData();
			}
			
			out.println(">>> Primary keys\n");
			rs = dmd.getPrimaryKeys(baza, schema, table);
			md = rs.getMetaData();
			if (rs != null && md != null) {
				printHeader();
				printData();
			}
		}

		out.close();
		
		System.out.println("Finished.");
	}

	private void makeTableList() throws SQLException {
		tableList = new ArrayList<String>();
		while (rs.next()) {
			tableList.add(rs.getString(3));
		}
	}

	private void printHeader() throws SQLException {
		StringBuffer str = new StringBuffer();
		for (int i = 1; i < md.getColumnCount(); i++) {
			String label = md.getColumnLabel(i);
			int w = 30;
			if (label.equals("PK_NAME") || label.equals("FK_NAME"))
				w = 40;
			str.append(String.format("%-" + w + "s", label));
		}
		out.println(str.toString());

		String line = "";
		for (int i = 0; i < str.length(); i++)
			line += "-";
		out.println(line);
	}

	private void printData() throws SQLException {
		StringBuffer str;
		while (rs.next()) {
			str = new StringBuffer();
			for (int i = 1; i < md.getColumnCount(); i++) {
				String label = md.getColumnLabel(i);
				int w = 30;
				if (label.equals("PK_NAME") || label.equals("FK_NAME"))
					w = 40;
				str.append(String.format("%-" + w + "s", rs.getString(i)));
			}
			out.println(str.toString());
		}
		out.println();
	}

	public static void main(String[] args) {
		try {
			new DatabaseInfo();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}